package com.so.web.controller.monitor;

import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.so.common.annotation.Log;
import com.so.common.core.controller.BaseController;
import com.so.common.core.domain.R;
import com.so.common.enums.BusinessType;
import com.so.common.utils.poi.ExcelUtil;
import com.so.framework.web.service.SysPasswordService;
import com.so.system.domain.SysLoginInfo;
import com.so.system.service.ISysLoginInfoService;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletResponse;
import java.util.Arrays;
import java.util.List;

/**
 * 系统访问记录
 *
 * @author system
 */
@RestController
@RequestMapping("/monitor/logininfor")
public class SysLoginInfoController extends BaseController {

    @Resource
    private ISysLoginInfoService loginInfoService;

    @Resource
    private SysPasswordService passwordService;

    @PreAuthorize("@ss.hasPermi('monitor:logininfor:list')")
    @GetMapping("/list")
    public R<Page<SysLoginInfo>> list(Page<SysLoginInfo> page, SysLoginInfo loginInfo) {
        Page<SysLoginInfo> result = loginInfoService.selectLoginInfoList(page,loginInfo);
        return R.ok(result);
    }

    @Log(title = "登录日志", businessType = BusinessType.EXPORT)
    @PreAuthorize("@ss.hasPermi('monitor:logininfor:export')")
    @PostMapping("/export")
    public void export(HttpServletResponse response, SysLoginInfo loginInfo) {
        List<SysLoginInfo> list = loginInfoService.selectLoginInfoList(loginInfo);
        ExcelUtil<SysLoginInfo> util = new ExcelUtil<>(SysLoginInfo.class);
        util.exportExcel(response, list, "登录日志");
    }

    @PreAuthorize("@ss.hasPermi('monitor:logininfor:remove')")
    @Log(title = "登录日志", businessType = BusinessType.DELETE)
    @DeleteMapping("/{infoIds}")
    public R<Void> remove(@PathVariable Long[] infoIds) {
        loginInfoService.removeBatchByIds(Arrays.asList(infoIds));
        return R.ok();
    }

    @PreAuthorize("@ss.hasPermi('monitor:logininfor:remove')")
    @Log(title = "登录日志", businessType = BusinessType.CLEAN)
    @DeleteMapping("/clean")
    public R<Void> clean() {
        loginInfoService.cleanLogininfor();
        return R.ok();
    }

    @PreAuthorize("@ss.hasPermi('monitor:logininfor:unlock')")
    @Log(title = "账户解锁", businessType = BusinessType.OTHER)
    @GetMapping("/unlock/{userName}")
    public R<Void> unlock(@PathVariable("userName") String userName) {
        passwordService.clearLoginRecordCache(userName);
        return R.ok();
    }
}
